<?php

class PartnerController extends AdminController {

    public $parentUrl;

    public function init() {
        parent::init();
        $this->parentUrl = $this->module->id . '/' . Yii::app()->getController()->getId();
    }

    public function actionView($id) {
        try {
            $this->render('view', array(
                'model' => $this->loadModel($id),
                'parentUrl' => $this->parentUrl
            ));
        } catch (Exception $e) {
            Yii::log("Invalid request. Please do not repeat this request again.");
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        }
    }

    public function actionIndex() {
        try {
            $data = Partner::model()->findAll();
            $this->render('index', array(
                'data' => $data,
                'parentUrl' => $this->parentUrl
            ));
        } catch (Exception $e) {
            Yii::log("Invalid request. Please do not repeat this request again.");
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        }
    }

    public function actionCreate() {
        try {
            $model = new Partner();
            if (isset($_POST['Partner'])) {
                $model->attributes = $_POST['Partner'];
                $object_file = CUploadedFile::getInstance($model, 'cover_image');
                $model->name = $_POST['Partner']['name'];
                $model->link = $_POST['Partner']['link'];
                if ($object_file !== null) {
                    $ran = rand(0, 999999999);
                    $fileName = trim(date('Y-m-d H-i-s') . $ran) . '.' . $object_file->extensionName;
                    $model['cover_image'] = $fileName;
                    $model->save();
                    $object_file->saveAs(Yii::app()->basePath . "/../data/cover/$fileName");
                }
                $model->validate();
                if (!$model->hasErrors()) {
                    if ($model->save($model)) {
                        $this->redirect(Yii::app()->createAbsoluteUrl('systems/partner/index'));
                    }
                }
            }
            $this->render('create', array(
                'model' => $model,
                'parentUrl' => $this->parentUrl
            ));
        } catch (Exception $e) {
            Yii::log("Invalid request. Please do not repeat this request again.");
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        }
    }

    public function actionUpdate($id) {
        try {
            $model = $this->loadModel($id);
            $old_cover = $model->cover_image;
            if (isset($_POST['Partner'])) {
                $model->attributes = $_POST['Partner'];
                $object_file = CUploadedFile::getInstance($model, 'cover_image');
                if ($object_file !== null) {
                    $ran = rand(0, 999999999);
                    $fileName = trim(date('Y-m-d H-i-s') . $ran) . '.' . $object_file->extensionName;
                    $model['cover_image'] = $fileName;
                    $model->save();
                    $object_file->saveAs(Yii::app()->basePath . "/../data/cover/$fileName");
                    if (file_exists(Yii::app()->basePath . "/../data/cover/$old_cover")) {
                        unlink(Yii::app()->basePath . "/../data/cover/$old_cover");
                    }
                }else{
                    $model['cover_image'] = $old_cover;
                }
                $model->validate();
                if (!$model->hasErrors()) {
                    if ($model->save($model)) {
                        $this->redirect(Yii::app()->createAbsoluteUrl('systems/partner/index'));
                    }
                }
            }
            $this->render('update', array(
                'model' => $model,
                'parentUrl' => $this->parentUrl
            ));
        } catch (Exception $e) {
            Yii::log("Invalid request. Please do not repeat this request again.");
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        }
    }

    public function actionDelete($id) {
        try {
            if ($model = $this->loadModel($id)) {
                if (file_exists(Yii::app()->basePath . "/../data/cover/$model->cover_image")) {
                    unlink(Yii::app()->basePath . "/../data/cover/$model->cover_image");
                }
                if ($model->delete()) {
                    $this->redirect(Yii::app()->createAbsoluteUrl('systems/partner/index'));
                }
            }
        } catch (Exception $e) {
            Yii::log("Invalid request. Please do not repeat this request again.");
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        }
    }

    public function loadModel($id) {
        try {
            $model = Partner::model()->findByPk($id);
            if ($model === null)
                throw new CHttpException(404, 'The requested page does not exist.');
            return $model;
        } catch (Exception $e) {
            Yii::log("Invalid request. Please do not repeat this request again.");
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        }
    }

}

?>
